package mo.offer_version2;

public class L16 {

    public double myPow(double x, int n) {
        int absn = Math.abs(n);
        double pow = compute(x, absn);
        if (n >= 0) {
            return pow;
        } else {
            return 1 / pow;
        }
    }

    private double compute(double x, int n) {
        if (n == 0) {
            return 1;
        }
        if (n == 1) {
            return x;
        }
        if (n == 2) {
            return x * x;
        }
        if (n % 2 == 0) {
            return compute(x*x, n / 2);
        } else {
            return compute(x*x, n / 2) * x;
        }

    }

}
